package util

import (
	"github.com/golang-module/carbon/v2"
	"strconv"
	"time"
)

const (
	DefaultDatetimeFormat = "Y-m-d H:i"
)

// FormatYmdHi 格式化为 2023-07-01 12:01
func FormatYmdHi(timestamp int32) string {
	return FormatByTimestamp(int64(timestamp), DefaultDatetimeFormat)
}

func FormatByTimestamp(timestamp int64, tpl string) string {
	if timestamp <= 0 {
		return ""
	}
	if tpl == "" {
		tpl = DefaultDatetimeFormat
	}
	return carbon.CreateFromTimestamp(timestamp).ToFormatString(tpl)
}

// FormatLineYmdByDate 将20230701 格式化为 2023-07-01
func FormatLineYmdByDate(date int32) string {
	if date <= 0 {
		return ""
	}
	//将20230701 格式化为 2023-07-01
	return carbon.CreateFromDate(int(date/10000), int(date/100%100), int(date%100)).ToFormatString("Y-m-d")
}

// GetTodayYmd 当前时间的 Ymd 格式
func GetTodayYmd() int32 {
	ymd := carbon.Now().ToFormatString("Ymd")
	ymdInt, _ := strconv.Atoi(ymd)
	//nolint
	return int32(ymdInt)
}

// GetNowTimestampInt32 获取当前时间戳
func GetNowTimestampInt32() int32 {
	return int32(time.Now().Unix())
}

// GetNowTimestamp 获取当前时间戳
func GetNowTimestamp[T int | int8 | int16 | int32 | int64]() T {
	return T(time.Now().Unix())
}
